{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 描述\n",
    "slice() 函数实现切片对象，主要用在切片操作函数里的参数传递。\n",
    "\n",
    "参考：https://www.youtube.com/watch?v=ajrtAuDg3yw\n",
    "\n",
    "# 语法\n",
    "slice 语法：\n",
    "```\n",
    "class slice(stop)\n",
    "class slice(start, stop[, step])\n",
    "```\n",
    "# 参数说明：\n",
    "\n",
    "+ `start` -- 起始位置\n",
    "+ `stop` -- 结束位置\n",
    "+ `step` -- 间距\n",
    "\n",
    "# 返回值\n",
    "返回一个切片对象。\n",
    "\n",
    "# 实例\n",
    "以下实例展示了 slice 的使用方法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5, 4, 3, 2, 1]\n",
      "[9, 8, 7, 6, 5, 4]\n",
      "[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n",
      "[0, 2, 4, 6, 8]\n"
     ]
    }
   ],
   "source": [
    "s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    "print(s[5:0:-1])\n",
    "print(s[:3:-1])\n",
    "print(s[9::-1])\n",
    "print(s[::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "首先定义 s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
    " \n",
    "Python中正索引是从0开始的，负索引从-1开始,索引1对应1, 而索引-1对应9.\n",
    " \n",
    "`s[5:0:-1]`: 1.step 为负数,从右往左输\n",
    "          2.从索引 5 开始 到索引0 结束, 但不包括索引0 , 输出为 [5, 4, 3, 2, 1]\n",
    " \n",
    "`s[:3:-1]`： 1.step 为负数,从右往左输出\n",
    "          2.step 为负数,[start]默认值为索引最大值, 即 9, 那么这个切片等价于s[9:3:-1]\n",
    "          3.从索引 9 开始 到索引3 结束, 但不包括索引3 , 输出为[9, 8, 7, 6, 5, 4]\n",
    " \n",
    "`s[9::-1]`:  1.step 为负数,从右往左输\n",
    "          2.step 为负数,[stop]默认值为0, 那么这个切片等价于s[9:0:-1],输出为[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n",
    " \n",
    "`s[::2]`:    1.step 为正数,从右往左输\n",
    "          2.step 为正数,start默认是0，stop默认是索引最大值,那么这个切片等价于s[0:9:2\n",
    "          3.step 为2表示走两步, 即中间间隔一个字符, 输出为[0, 2, 4, 6, 8]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
